class Solution {
public:
    int maximumUniqueSubarray(vector<int>& nums) {
        int l = 0, r = 1, sum = nums[0];
        unordered_set<int> st;
        st.insert(nums[0]);
        int ans = nums[0], n = nums.size();
        while (r < n) {
            while (l < r && st.count(nums[r])) {
                st.erase(nums[l]);
                sum -= nums[l];
                l++;
            }
            sum += nums[r];
            st.insert(nums[r]);
            ans = max(ans, sum);
            ++r;
        }
        return ans;
    }
};